package com.example.httpservlet;

import jakarta.servlet.Servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(urlPatterns = "/httpServletExample")
public class HttpServletExample  extends HttpServlet {

   //处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet~~~");
        String method = req.getMethod();
        System.out.println("method："+method);

        resp.setContentType("text/html;charset=utf-8") ;
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("method："+method);
        resp.setCharacterEncoding("UTF-8");
        //显示html界面的逻辑代码
        PrintWriter writer = resp.getWriter();
        writer.write("\r\n");
        writer.write("\r\n");
        writer.write("<html>\r\n");
        writer.write("<head>\r\n");
        writer.write("    <title></title>\r\n");
        writer.write("</head>\r\n");
        writer.write("<body>\r\n");
        writer.write("<h1>"+"HttpServletExample GET请求成功"+"<h1>");
        writer.write("\r\n");
        writer.write("</body>\r\n");
        writer.write("</html>\r\n");

    }

    //处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost~~~");
        String method = req.getMethod();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("method："+method);
        resp.setCharacterEncoding("UTF-8");
        //显示html界面的逻辑代码
        PrintWriter writer = resp.getWriter();
        writer.write("\r\n");
        writer.write("\r\n");
        writer.write("<html>\r\n");
        writer.write("<head>\r\n");
        writer.write("    <title>登录成功</title>\r\n");
        writer.write("</head>\r\n");
        writer.write("<body>\r\n");
        writer.write("<h1>"+username+",Login success"+"<h1>");
        writer.write("\r\n");
        writer.write("</body>\r\n");
        writer.write("</html>\r\n");

    }
}
